package org.zhiyootech.resources;

import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.zhiyootech.base.annotation.Login;
import org.zhiyootech.base.sdkimpl.ZhiYooWxPay;
import org.zhiyootech.domain.PayRequest;
import org.zhiyootech.sdk.wxpay.WXPay;
import org.zhiyootech.sdk.wxpay.WXPayUtil;

import javax.annotation.security.PermitAll;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Path("/pay")
@RequestScoped
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class PayResource {

    @Inject
    ZhiYooWxPay zhiYooWxPay;

    @POST
    @Path("/wp1")
    @PermitAll
    @Login
    public void weChartPayOrder(PayRequest payRequest) {
        try {
            WXPay wxPay = new WXPay(zhiYooWxPay);
            HashMap<String, String> orderParam = Maps.newHashMap();
            orderParam.put("nonce_str", WXPayUtil.generateNonceStr());
            orderParam.put("body", "智友订单支付");
            orderParam.put("out_trade_no", "订单号");
            orderParam.put("total_fee", "1");// 金额，单位：分
            orderParam.put("spbill_create_ip", "127.0.0.1");// 商户IP地址
            orderParam.put("notify_url", "https://127.0.0.1/test");// 通知跳转地址
            orderParam.put("trade_type", "JSAPI");// 固定
            orderParam.put("openid", ""); //
            Map<String, String> res = wxPay.unifiedOrder(orderParam);
            log.info("微信返回订单：{}", res);
        } catch (Exception e) {
            log.error("支付失败", e);
        }
    }

}
